package solution._0100._0102;

import common.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by chen_snow
 * Date: 2021/6/23 22:47
 * Description:  二叉树的层序遍历
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        ArrayList<List<Integer>> result = new ArrayList<>();

        if (root == null) {
            return result;
        }

        Queue<TreeNode> data = new LinkedList<>();
        data.offer(root);

        while (!data.isEmpty()) {
            int size = data.size();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode tmp = data.poll();
                if (tmp.left != null) {
                    data.offer(tmp.left);
                }
                if (tmp.right != null) {
                    data.offer(tmp.right);
                }
                list.add(tmp.val);
            }
            result.add(list);
        }
        return result;
    }
}
